#include "theater.h"
#include "synch.h"
#include "system.h"



//the manager told the ticketTaker to work
void movieTechnician_startWork(unsigned int movTech_workno)
{
    DBGPRINT(DEBUG_TRACE,"movTech_workno %d is going to work\n",movTech_workno);
    while(1)
    {
        //step 1: check the manager ask me play the movie or not
        /*      movieReadyLock->Acquire();
              if(movie_state!=movie_state)
                  movieReady_cv->Wait(movieReadyLock);//if not, I need to wait for the manger's call
              movieReadyLock->Release();
              //step 2: need to check the room is full or not*/
check:
        DBGPRINT(DEBUG_TRACE,"movTech_workno wait: RoomFull:%d, movie_state %d\n",movTech_workno,RoomFull,movie_state);

        DBGPRINT(DEBUG_TRACE,"movTech_workno wait: RoomFull:%d, movie_state %d\n",movTech_workno,RoomFull,movie_state);
        roomfullLock->Acquire();
        DBGPRINT(DEBUG_TRACE,"movTech_workno wait: RoomFull:%d, movie_state %d\n",movTech_workno,RoomFull,movie_state);
        DBGPRINT(DEBUG_TRACE,"movTech_workno wait: RoomFull:%d, movie_state %d\n",movTech_workno,RoomFull,movie_state);
        DBGPRINT(DEBUG_TRACE,"movTech_workno wait: RoomFull:%d, movie_state %d\n",movTech_workno,RoomFull,movie_state);
        if(ROOMFULL!=RoomFull||MOVIE_READY!=movie_state)
        {
            DBGPRINT(DEBUG_TRACE,"movTech_workno wait: RoomFull:%d, movie_state %d\n",movTech_workno,RoomFull,movie_state);
            DBGPRINT(DEBUG_TRACE,"movTech_workno wait: RoomFull:%d, movie_state %d\n",movTech_workno,RoomFull,movie_state);
            roomfullLock->Release();
            movieTechMsgLock->Acquire();
            DBGPRINT(DEBUG_TRACE,"movTech_workno wait: RoomFull:%d, movie_state %d\n",movTech_workno,RoomFull,movie_state);
            movieTechMsg_cv->Wait(movieTechMsgLock);
            DBGPRINT(DEBUG_TRACE,"movTech_workno wait: RoomFull:%d, movie_state %d\n",movTech_workno,RoomFull,movie_state);
            movieTechMsgLock->Release();
            DBGPRINT(DEBUG_TRACE,"movTech_workno wake up: RoomFull:%d, movie_state %d\n",movTech_workno,RoomFull,movie_state);
            goto check;
        }
        seatsRsvLock->Acquire();
        DBGPRINT(DEBUG_TRACE,"movTech_workno wait: RoomFull:%d, movie_state %d\n",movTech_workno,RoomFull,movie_state);
        DBGPRINT(DEBUG_TRACE,"movTech_workno wait: RoomFull:%d, movie_state %d\n",movTech_workno,RoomFull,movie_state);
        // moviePlayLock->Acquire();
        movie_stateLock->Acquire();
        movie_state=MOVIE_PLAYING;
        DBGPRINT(DEBUG_TRACE,"movTech_workno wait: RoomFull:%d, movie_state %d\n",movTech_workno,RoomFull,movie_state);
        movie_stateLock->Release();
        // moviePlayLock->Release();
        DBGPRINT(DEBUG_TRACE,"movTech_workno wait: RoomFull:%d, movie_state %d\n",movTech_workno,RoomFull,movie_state);
        DBGPRINT(DEBUG_TRACE,"movTech_workno %d play the movie, peopel %d\n",movTech_workno,seatsReserved);
		printf("The MovieTechnician has started the movie.\n");
        //step 3: play the movie
//        int time=200+rand()%100;
        for(int i=0; i<350; i++)
            currentThread->Yield();
        DBGPRINT(DEBUG_TRACE,"movTech_workno %d movie is over\n",movTech_workno);
		printf("The MovieTechnician has ended the movie.\n");
        //step 3: wake all the customers up
        movie_stateLock->Acquire();
        DBGPRINT(DEBUG_TRACE,"movTech_workno wait: RoomFull:%d, movie_state %d\n",movTech_workno,RoomFull,movie_state);
        movie_state=MOVIE_OVER;
        DBGPRINT(DEBUG_TRACE,"movTech_workno %d Broadcast\n");
        movieState_cv->Broadcast(movie_stateLock);
        DBGPRINT(DEBUG_TRACE,"movTech_workno wait: RoomFull:%d, movie_state %d\n",movTech_workno,RoomFull,movie_state);
        movie_stateLock->Release();
        seatsReserved=0;
        seatsRsvLock->Release();
        DBGPRINT(DEBUG_TRACE,"movTech_workno wait: RoomFull:%d, movie_state %d\n",movTech_workno,RoomFull,movie_state);
        //step 4: clear the room and change the state
        roomfullLock->Acquire();
        DBGPRINT(DEBUG_TRACE,"movTech_workno wait: RoomFull:%d, movie_state %d\n",movTech_workno,RoomFull,movie_state);
        RoomFull=ROOMNOTFULL;//change the roomfull sate
        roomfullLock->Release();
        DBGPRINT(DEBUG_TRACE,"movTech_workno wait: RoomFull:%d, movie_state %d\n",movTech_workno,RoomFull,movie_state);
        DBGPRINT(DEBUG_TRACE,"movTech_workno wait: RoomFull:%d, movie_state %d\n",movTech_workno,RoomFull,movie_state);
        DBGPRINT(DEBUG_TRACE,"movTech_workno wait: RoomFull:%d, movie_state %d\n",movTech_workno,RoomFull,movie_state);
        //moviePlayLock->Acquire();
        //movie_state=MOVIE_NOPLAYING;
        DBGPRINT(DEBUG_TRACE,"movTech_workno %d Broadcast\n");
		printf("The MovieTechnician has told all customers to leave the theater room.\n");
        //moviePlay_cv->Broadcast(moviePlayLock);
        //moviePlayLock->Release();
    }
};


